import { login, getInfo, getUserInfoById } from '@/api/user'
import { getToken, setToken, removeToken, setTimes } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}

  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }

  },
  actions: {
    async userLogin(context, payload) {
      const res = await login(payload)
      console.log(res)
      context.commit('setToken', res.data)
      setTimes()
    },
    async userInfo(context) {
      const res = await getInfo()
      console.log(res)
      const info = await getUserInfoById(res.data.userId)
      console.log(info)
      context.commit('setUserInfo', { ...res.data, ...info.data })
      // 把用户页面几倍的权限信息返回到路由守卫中
      return res.data.roles.menus
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  }
}

